/*
 * Copyright (c) 2012 Android Dev Freelancer Team. All Rights Reserved.
 */
package com.devgroupvn.dota;

import java.util.ArrayList;

import view.ActivitySearchYoutube;
import view.ItemListLayout;
import view.ListView1Page;
import view.NewsPage;
import view.ViewPagerIndicator;
import view.ViewPagerIndicator.PageInfoProvider;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.devgroupvn.dota.hero.PageHero;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class ViewPagerActivity extends Activity
{

	/** Control Element */
	public WebView mWebView;
	public ListView mListView;
	public Button btnMore;
	public ProgressBar pb;
	public static String[] mValText;

	public static ArrayList<Post> listpost;
	MyPagerAdapter adapter;
	// async task
	public Async async;

	/** Web Items */
	public static WebContent mWebContent;
	ViewPagerIndicator mIndicator;
	Context mContext;
	public static String strLayout = "MAINAPPLICATION";
	public final static String LAYOUT_SHARE = "SHARESNS";
	public final static String LAYOUT_MAIN = "MAINAPPLICATION";
	public static boolean hasApp = false;

	ViewPager viewPager;
	AdView adView;

	String[] Tab = new String[]
	{ "VODS", "NEWS", "HEROES", "CHANEL","ITEMS" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.main_viewpager);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		adView = (AdView) findViewById(R.id.adView);
		adapter = new MyPagerAdapter(this);

		viewPager.setAdapter(adapter);
		mIndicator = (ViewPagerIndicator) findViewById(R.id.indicator);
		viewPager.setOnPageChangeListener(mIndicator);
		mIndicator.init(0, adapter.getCount(), adapter);

		Resources res = getResources();
		Drawable prev = res.getDrawable(R.drawable.indicator_prev_arrow);
		Drawable next = res.getDrawable(R.drawable.indicator_next_arrow);
		mIndicator.setFocusedTextColor(new int[]
		{ 255, 255, 255 });
		mIndicator.setBackgroundColor(getResources().getColor(R.color.reddota));
		mIndicator.setUnfocusedTextColor(new int[]
		{ 102, 102, 102 });
		// Set images for previous and next arrows.
		mIndicator.setArrows(prev, next);

		mIndicator.setOnClickListener(new OnIndicatorClickListener());

		mWebContent = new WebContent();

		viewPager.setOnPageChangeListener(mIndicator);
		// adView = (AdView) findViewById(R.id.adView);
		new AsyncCheckApp(this).execute();
		if (isNetworkAvailable())
		{
			initiation();
			// loadContent();
		}
		else
		{
			NetWorkFalse();
		}
		setupAdwhirl();

		/*adView.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				adView.setVisibility(View.GONE);
			}
		});*/

	}

	class OnIndicatorClickListener implements ViewPagerIndicator.OnClickListener
	{
		@Override
		public void onCurrentClicked(View v)
		{
		}

		@Override
		public void onNextClicked(View v)
		{
			viewPager.setCurrentItem(Math.min(adapter.getCount() - 1, mIndicator.getCurrentPosition() + 1));
		}

		@Override
		public void onPreviousClicked(View v)
		{
			viewPager.setCurrentItem(Math.max(0, mIndicator.getCurrentPosition() - 1));
		}

	}

	private class MyPagerAdapter extends PagerAdapter implements PageInfoProvider
	{

		private ArrayList<LinearLayout> views;

		public MyPagerAdapter(Context context)
		{
			views = new ArrayList<LinearLayout>();
			views.add(new ListView1Page(mContext, ViewPagerActivity.this));
			views.add(new NewsPage(mContext, ViewPagerActivity.this));
			views.add(new PageHero(mContext));
			views.add(new ActivitySearchYoutube(mContext, ViewPagerActivity.this));
			views.add(new ItemListLayout(mContext));
		}

		@Override
		public void destroyItem(View view, int arg1, Object object)
		{
			((ViewPager) view).removeView((LinearLayout) object);
		}

		@Override
		public void finishUpdate(View arg0)
		{

		}

		@Override
		public int getCount()
		{
			return views.size();
		}

		@Override
		public Object instantiateItem(View view, int position)
		{
			View myView = views.get(position);
			((ViewPager) view).addView(myView);
			return myView;
		}

		@Override
		public boolean isViewFromObject(View view, Object object)
		{
			return view == object;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1)
		{

		}

		@Override
		public Parcelable saveState()
		{
			return null;
		}

		@Override
		public void startUpdate(View arg0)
		{

		}

		@Override
		public String getTitle(int pos)
		{
			return Tab[pos];
		}

	}

	public static int page = 0;

	private void initiation()
	{

	}

	@Override
	protected void onPause()
	{
		//
		if (async != null && !async.isCancelled())
		{
			async.isCancelled();
		}
		super.onPause();
	}

	@Override
	public void onBackPressed()
	{

		{
			AlertDialog.Builder builder = new AlertDialog.Builder(ViewPagerActivity.this);
			builder.setTitle("JoinDota").setMessage(R.string.close_app).setCancelable(true)
					.setPositiveButton(getString(R.string.cancel), new DialogInterface.OnClickListener()
					{

						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.cancel();
						}
					}).setNegativeButton(getString(R.string.ok), new DialogInterface.OnClickListener()
					{

						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							android.os.Process.killProcess(android.os.Process.myPid());
						}
					}).create().show();
		}
	}

	private void setupAdwhirl()
	{
		try
		{
			final Handler handle = new Handler();
			final Runnable run = new Runnable()
			{

				@Override
				public void run()
				{
					AdRequest request = new AdRequest();
					adView.loadAd(request);
					handle.postDelayed(this, 5000);

				}
			};
			handle.postDelayed(run, 5000);
		}
		catch (Exception e)
		{

		}
	}

	private boolean isNetworkAvailable()
	{

		ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connMgr.getActiveNetworkInfo();
		return (info != null && info.isConnected());
	}

	public static ProgressDialog progressDialog;

	private class AsyncCheckApp extends AsyncTask<Void, Void, Void>
	{
		Context aContext;
		boolean _hasApp;

		AsyncCheckApp(Context _context)
		{
			aContext = _context;
		}

		@Override
		protected Void doInBackground(Void... params)
		{
			CheckAppInstall check = new CheckAppInstall(aContext);
			_hasApp = check.checkApp(aContext);
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			hasApp = _hasApp;
		}

	}

	private void NetWorkFalse()
	{
		Context _context = (Context) this;
		AlertDialog.Builder askToDelete = new AlertDialog.Builder(_context);
		askToDelete.setMessage(getString(R.string.connection_error));
		askToDelete.setPositiveButton(getString(R.string.exists), new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		AlertDialog dialog = askToDelete.create();
		dialog.show();
	}

}
